package com.lsy.seckill.RabbitMQ;

import cn.hutool.json.JSONUtil;
import com.lsy.seckill.pojo.SeckillMessage;
import com.lsy.seckill.pojo.User;
import com.lsy.seckill.service.GoodsService;
import com.lsy.seckill.service.OrderService;
import com.lsy.seckill.vo.SeckillGoodsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class SeckillReceiver {
    @Resource
    private GoodsService goodsService;
    @Resource
    private OrderService orderService;
    @RabbitListener(queues = "queue")
    public void receiverSeckillMessage(String message){
        log.info("接受到的消息："+message);
        SeckillMessage seckillMessage = JSONUtil.toBean(message, SeckillMessage.class);
        User user = seckillMessage.getUser();
        Long goodsId = seckillMessage.getGoodsId();
        SeckillGoodsVo goodsVo = goodsService.findSeckillGoodsVoByGoodsId(goodsId);
        orderService.seckill(user,goodsVo);
    }
}
